﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crawler.Model;
using Husb.Util;

using Crawler.BusinessActions;

namespace WinFromsClient
{
    public partial class UrlForm : Form
    {
        private TaskItemInfo taskItem;
        private DataViewMode mode;
        public UrlForm(TaskItemInfo taskItem, DataViewMode mode)
        {
            InitializeComponent();

            //this.dgvTaskItem.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            WinUtil.PopulateListBox(pageCategoryComboBox, ProjectConstants.PageCategory, false);
            this.pageCategoryComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.taskItemInfoBindingSource, "PageCategory", true));

            this.taskItemInfoBindingSource.DataSource = taskItem;
            this.taskItem = taskItem;

            this.captureRuleBindingSource.DataSource = taskItem.CaptureRules;
        }

        private void btnAddRule_Click(object sender, EventArgs e)
        {
            OpenCaptureRuleForm(DataViewMode.Insert);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.taskItemInfoBindingSource.EndEdit();

            if (mode == DataViewMode.Edit)
            {
                TaskItem.Update(taskItem);
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            TestForm form = new TestForm();

            string url = taskItem.Url;
            if(!String.IsNullOrEmpty(url))
            {
                form.LoadHtml(Utilities.GetPageHTML(url));
                form.ShowDialog();
            }
        }

        private void btnEditRule_Click(object sender, EventArgs e)
        {
            OpenCaptureRuleForm(DataViewMode.Edit);
        }

        private void OpenCaptureRuleForm(DataViewMode mode)
        {
            if (taskItem.IsValid)
            {
                CaptureRuleInfo cr;
                if (mode == DataViewMode.Edit)
                {
                    cr = this.captureRuleBindingSource.Current as CaptureRuleInfo;
                }
                else
                {
                    cr = new CaptureRuleInfo();
                    cr.Id = Guid.NewGuid();
                    cr.TaskItem = taskItem;
                    cr.Url = this.taskItem.Url;
                    cr.TaskItemId = this.taskItem.Id;
                }
                CaptureRuleForm form = new CaptureRuleForm(cr);
                if (DialogResult.OK == form.ShowDialog())
                {
                    taskItem.CaptureRules.Add(cr);

                    if (mode == DataViewMode.Edit)
                    {
                        CaptureRule.Update(cr);
                    }
                }
            }
            else
            {
                errorProvider1.SetError(urlTextBox, taskItem.ValidationMessage);
            }
        }

        private void btnDeleteRule_Click(object sender, EventArgs e)
        {

        }
    }
}
